package zjw.springboot2.test.controller;

import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
 
@Component
@ServerEndpoint("/websocketurl")//设置websocket连接url，就是前端创建websocket需要提供的url
public class MyWebSocketService {


    @OnOpen
    public void onOpen(Session session) {
        System.out.println("websocket isOpen !");
    }

    @OnClose
    public void OnClose(Session session) {
        System.out.println("websocket isClose !");
    }

    @OnError
    public void OnError(Session session, Throwable error) {
        System.out.println(error.getMessage());
    }

    @OnMessage
    public void OnMessage(Session session, String message) {
        try {
            session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
